//#define __DEBUG__
#define THIS_FILE "x_getmtpriority.sqf"
#include "x_setup.sqf"

#define __getPos \
_try = 100; \
_poss = [_target_array2 select 0, _target_array2 select 2,200] call FUNC(getranpointcirclebigouter);\
while {count _poss == 0 && _try > 0} do {\
	_poss = [_target_array2 select 0, _target_array2 select 2,200] call FUNC(getranpointcirclebigouter);\
	if (count _poss > 0 && {((count (_poss nearRoads 7) > 0) || ([_poss, 8] call FUNC(getslope)) > 0.6)}) then {_poss = []};\
	__DEC(_try); \
	sleep 0.01;\
}

if !(call FUNC(checkSHC)) exitWith {};
__TRACE_1("_this","_this")

private ["_wp_array","_poss","_try"];
_wp_array = _this;
_poss = [];

__TargetInfo;

__TRACE_1("TargetInfo","_target_array2")

__getPos;

GVAR(priority_trigger) = objNull;
GVAR(priority_objects) = [];
GVAR(priority_resolved) = false;

if (count _poss > 0) then {
	_activation = format["nul = 0 spawn { %1 execVM 'x_mtmissions\x_createmtpriority.sqf';  sleep 5; deleteVehicle %2}", _poss, QGVAR(priority_trigger)];
	_condition = format["%1 && (this || %2) && (%3 >= %4)",QGVAR(main_target_ready), QGVAR(mt_spotted),QUOTE(call FUNC(PlayersNumber)),QGVAR(MTPriorityObjectiveThresold)];
	__TRACE_2("priority_trigger","_condition","_activation");
	GVAR(priority_trigger) = [(_target_array2 select 0), [(_target_array2 select 2) + 100, (_target_array2 select 2) + 100, 0, false], [GVAR(own_side_trigger), "PRESENT", false], [_condition, _activation, ""]] call FUNC(CreateTrigger);
	if (isMultiPlayer) then {
		GVAR(priority_trigger) setTriggerTimeout [20,30,40,true];
	};
#ifdef __DEBUG__	
	["mt_priority", _poss, "ICON","ColorRed",[1,1]," MTP",0,"hd_flag"] call FUNC(CreateMarkerLocal);
#endif
};